package cn.agiledata.bank.myspace.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountMessage;
import cn.agiledata.bank.hibernateBean.MessageCustomization;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.myspace.dao.IMySpaceDao;

public class MessageCustomServiceImpl implements MessageCustomService {

	private IMySpaceDao mySpaceDao;

	public IMySpaceDao getMySpaceDao() {
		return mySpaceDao;
	}

	public void setMySpaceDao(IMySpaceDao mySpaceDao) {
		this.mySpaceDao = mySpaceDao;
	}
	
	public List findAllMessageCustoms() {
		return mySpaceDao.getAllMessageCustoms();
	}

	public Account saveAllMessageCustoms(String[] emails, String[] mobiles,
			Account account) {
		// 获得所有可定制消息，用户已定制消息
		List messageCustoms = this.findAllMessageCustoms();
		Set accountMessages = account.getAccountMessages();
		
		Set tempMessages = new HashSet();

		// 根据所有可定制消息查询用户是否已经定制此消息
		Iterator mci = messageCustoms.iterator();
		
		for (; mci.hasNext();) {
			MessageCustomization messageCustom = (MessageCustomization) mci
					.next();
			String custom = "0";//标志是否已经定制过 0未定制，1已经定制
			Iterator ami = accountMessages.iterator();
			for (; ami.hasNext();) {
				AccountMessage accountMessage = (AccountMessage) ami.next();
				// 判断用户是否已经定制过此消息
				if (messageCustom.getSetId().equals(
						accountMessage.getMessageCustomization().getSetId())) {
					custom = "1";
					// 如果定制过和页面数据进行比较(先email再mobile)
					String email = "0";
					for (int i = 0; i < (emails == null ? 0 : emails.length); i++) {
						if (messageCustom.getSetId().equals(emails[i])) {
							email = "1";
							accountMessage.setEmailValidFlag("Y");
							break;
						}	
					}
					if("0".equals(email)){
						accountMessage.setEmailValidFlag("N");
					}
					
					String moblie = "0";
					for (int i = 0; i < (mobiles == null ? 0 : mobiles.length); i++) {
						if (messageCustom.getSetId().equals(mobiles[i])) {
							moblie = "1";
							accountMessage.setMobileValidFlag("Y");
							break;
						}	
					}
					if("0".equals(moblie)){
						accountMessage.setMobileValidFlag("N");
					}
					
					break;
				}
			}
			if("0".equals(custom)){
				//如果没有定制过此消息进行，定制操作
				
				AccountMessage accountMessageTemp = new AccountMessage();
				WeatherUtil.fillCreateAndUpdateInfo(accountMessageTemp, account
						.getId().toString());
				accountMessageTemp.setMessageCustomization(messageCustom);
				accountMessageTemp.setVersion(new Long(1));
				accountMessageTemp.setAccount(account);
				String email = "0";
				for (int i = 0; i < (emails == null ? 0 : emails.length); i++) {
					if (messageCustom.getSetId().equals(emails[i])) {
						email = "1";
						accountMessageTemp.setEmailValidFlag("Y");
						break;
					}
				}
				if("0".equals(email)){
					accountMessageTemp.setEmailValidFlag("N");
				}

				String moblie = "0";
				for (int i = 0; i < (mobiles == null ? 0 : mobiles.length); i++) {
					if (messageCustom.getSetId().equals(mobiles[i])) {
						moblie = "1";
						accountMessageTemp.setMobileValidFlag("Y");
						break;
					}	
				}
				if("0".equals(moblie)){
					accountMessageTemp.setMobileValidFlag("N");
				}
				tempMessages.add(accountMessageTemp);
			}
		}
		account.getAccountMessages().addAll(tempMessages);
		return account;
	}
	
	 public boolean ifSendMail(Account account, String type) {
	        if (!Constant.ACCOUNT_STATE_NORMAL.equals(account.getEmStatus())) {
	            return false;
	        } else {
	  
	        	 List list = mySpaceDao.getAccountMessage(account.getId().toString(), type);
	            if (list.size() == 0) {
	                MessageCustomization rule = mySpaceDao.getMessageCustomization(new Long(type));
	                if (rule.getEmailDefault().equals(Constant.INSIDE_STATUS_NO)) {
	                    return false;
	                } else {
	                    return true;
	                }

	            } else {
	                AccountMessage am = (AccountMessage) list.get(0);
	                if (am.getEmailValidFlag().equals(Constant.INSIDE_STATUS_NO)) {
	                    return false;
	                } else {
	                    return true;
	                }
	            }
	        }
	        // return true;
	    }


	    public boolean ifSendMobile(Account account, String type) {

	        if (!Constant.ACCOUNT_STATE_NORMAL.equals(account.getMobStatus())) {
	            return false;
	        } else {
	            
	            List list = mySpaceDao.getAccountMessage(account.getId().toString(), type);
	            if (list.size() == 0) {
	                MessageCustomization rule = mySpaceDao.getMessageCustomization(new Long(type));
	                if (rule.getMobileDefault().equals(Constant.INSIDE_STATUS_NO)) {
	                    return false;
	                } else {
	                    return true;
	                }

	            } else {
	                AccountMessage am = (AccountMessage) list.get(0);
	                if (am.getMobileValidFlag().equals(Constant.INSIDE_STATUS_NO)) {
	                    return false;
	                } else {
	                    return true;
	                }
	            }
	        }
	        // return false;
	    }
	
}
